package com.taikang.hmp.frame.user.action.intface;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.taikang.hmp.frame.common.ErrorConstant;
import com.taikang.hmp.frame.common.TKException;
import com.taikang.hmp.frame.common.datastructre.Dto;
import com.taikang.hmp.frame.common.datastructre.support.BaseDto;
import com.taikang.hmp.frame.common.datastructre.support.ResponseDto;
import com.taikang.hmp.frame.common.web.action.BaseAction;
import com.taikang.hmp.frame.common.web.session.AppSessionFactory;
import com.taikang.hmp.frame.common.web.session.SessionBean;

/**
 * User Action author:qrp time:2015年7月22日 下午5:11:04
 */
public class UserAction extends BaseAction {
	private static Logger logger = LoggerFactory.getLogger(UserAction.class);

	public String executeAction() {
		if ("saveBaseinfo".equals(requestDto.getMethod())) {
			return saveBaseinfo();
		} else if ("getBaseinfo".equals(requestDto.getMethod())) {
			return getBaseinfo();
		} else if ("getBmi".equals(requestDto.getMethod())) {
			return getBmi();
		} else {
			throw new TKException("no support method:" + requestDto.getMethod());
		}
	}

	/**
	 * 返回BMI
	 */
	public String getBmi() {
		ResponseDto responseDto = new ResponseDto();

		// 获取参数
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		String uid = paramDto.getAsString("uid");
		String height = paramDto.getAsString("height");
		String weight = paramDto.getAsString("weight");
		// 寻找session
		String sessionId = requestDto.getSessionid();
		SessionBean sessionBean = AppSessionFactory.getInstance(
				requestDto.getRequestsource()).isSessionTimeout(sessionId);
		if (sessionBean != null) {
			double bmi = 0;
			bmi = Math.round(Double.parseDouble(weight)
					/ Math.pow(Double.parseDouble(height) / 100, 2) * 10) / 10.0;
			// 保存以及修改及时更新session中的信息,以确保最新
			sessionBean = userservice.getCustomerBasicInfo(uid, sessionBean);
			AppSessionFactory.getInstance(requestDto.getRequestsource())
					.putSession(sessionId, sessionBean);
			Dto dto = new BaseDto();
			dto.put("result", bmi);
			responseDto.pushResultDto(dto);
		} else {
			responseDto.addErrorCode(ErrorConstant.SESSION_TIMEOUT);
		}

		super.setDataMap(responseDto);
		return SUCCESS;
	}

	/**
	 * 保存基本信息
	 */
	public String saveBaseinfo() {
		ResponseDto responseDto = new ResponseDto();

		// 获取参数
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		String uid = paramDto.getAsString("uid");

		// 寻找session
		String sessionId = requestDto.getSessionid();
		SessionBean sessionBean = AppSessionFactory.getInstance(
				requestDto.getRequestsource()).isSessionTimeout(sessionId);
		if (sessionBean != null) {
			userservice.saveBasicInfo(paramDto);
			// 保存以及修改及时更新session中的信息,以确保最新
			sessionBean = userservice.getCustomerBasicInfo(uid, sessionBean);
			AppSessionFactory.getInstance(requestDto.getRequestsource())
					.putSession(sessionId, sessionBean);
		} else {
			responseDto.addErrorCode(ErrorConstant.SESSION_TIMEOUT);
		}

		super.setDataMap(responseDto);
		return SUCCESS;
	}

	/**
	 * 获取基本信息
	 */
	public String getBaseinfo() {
		ResponseDto responseDto = new ResponseDto();

		// 寻找session
		String sessionId = requestDto.getSessionid();
		SessionBean sessionBean = AppSessionFactory.getInstance(
				requestDto.getRequestsource()).isSessionTimeout(sessionId);
		if (sessionBean != null) {
			if (sessionBean.getCustomerInfo() == null) {
				responseDto
						.addErrorCode(ErrorConstant.HD_CUSTOMER_BASE_DATA_NULL);
			} else {
				if (!StringUtils.isEmpty(sessionBean.getCustomerInfo()
						.getHeight())) {
					Dto dto = new BaseDto();

					dto.put("mobile", sessionBean.getCustomerInfo().getMobile());
					dto.put("name", sessionBean.getCustomerInfo().getName());
					dto.put("sex", sessionBean.getCustomerInfo().getSex());
					dto.put("birthday", sessionBean.getCustomerInfo()
							.getBirthday());
					dto.put("height", sessionBean.getCustomerInfo().getHeight());
					dto.put("weight", sessionBean.getCustomerInfo().getWeight());
					dto.put("usertype", sessionBean.getAuthUserInfo()
							.getUser_type());
					dto.put("breakfirst", sessionBean.getCustomerInfo()
							.getBreakfirst());
					dto.put("lunch", sessionBean.getCustomerInfo().getLunch());
					dto.put("dinner", sessionBean.getCustomerInfo().getDinner());
					dto.put("sleep", sessionBean.getCustomerInfo().getSleep());
					dto.put("sugar", sessionBean.getCustomerInfo().getSugar());
					dto.put("hemoglobin", sessionBean.getCustomerInfo()
							.getHemoglobin());
					dto.put("sugarafter", sessionBean.getCustomerInfo()
							.getSugarafter());
					responseDto.pushResultDto(dto);
				} else {
					responseDto
							.addErrorCode(ErrorConstant.HD_CUSTOMER_BASE_DATA_NULL);
				}
			}
		} else {
			responseDto.addErrorCode(ErrorConstant.SESSION_TIMEOUT);
		}
		super.setDataMap(responseDto);
		return SUCCESS;
	}

}
